//
//  vistorView.swift
//  新浪微博
//
//  Created by mac on 15/11/27.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
@objc protocol vistorViewLoginDelegate: NSObjectProtocol{
    //登录协议方法
   optional func userWillLogin()
    //注册的协议方法
   optional func userWillRegister()

}


class vistorView: UIView {
    //提供代理对象
   weak var vistorViewDelegation: vistorViewLoginDelegate?
    
    func setWithInfo(tiptext: String,imagename: String?){
        titleLabel.text = tiptext;
        if let name = imagename{
            cycleView.image = UIImage(named: name)
            houseView.hidden = true
            bringSubviewToFront(cycleView)
        }else{
        //首页的圆圈视图,添加动画
            let animate = CABasicAnimation(keyPath: "transform.rotation")
            animate.repeatCount = MAXFLOAT
            animate.duration = 2.0
            animate.toValue = 2 * M_PI
            //完成动画或当视图处于非活跃状态,将动画移除
            animate.removedOnCompletion = false
            cycleView.layer.addAnimation(animate, forKey: nil)
        }
       
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        addSubview(cycleView)
        addSubview(backView)
        //设置布局 VFL进行自动布局
        //使用VFL手动添加约束,必须将frame 设置视图的位置的属性关闭
        cycleView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: cycleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: cycleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        //房子视图
        addSubview(houseView)
        houseView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint( NSLayoutConstraint(item: houseView, attribute: .CenterX, relatedBy: .Equal, toItem: cycleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterY, relatedBy: .Equal, toItem: cycleView, attribute: .CenterY, multiplier: 1, constant: 0))
        //标签
        addSubview(titleLabel)
        
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .CenterX, relatedBy: .Equal, toItem: cycleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .CenterY, relatedBy: .Equal, toItem: cycleView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute:.Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute:.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))

        //添加登录按钮
        addSubview(loginBtn)
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute:.Left, relatedBy: .Equal, toItem: titleLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute:.Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1, constant: 30))
         addConstraint(NSLayoutConstraint(item: loginBtn, attribute:.Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
         addConstraint(NSLayoutConstraint(item: loginBtn, attribute:.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        //添加注册按钮
        addSubview(registerBtn)
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute:.Right, relatedBy: .Equal, toItem: titleLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute:.Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1, constant: 30))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute:.Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute:.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        //遮盖视图
        backView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options:[] , metrics: nil, views: ["maskView": backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-(-35)-[regBtn]", options:[] , metrics: nil, views: ["maskView": backView,"regBtn": registerBtn]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加按钮点击事件
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
    }
    
   @objc private func loginBtnClick(){
   vistorViewDelegation?.userWillLogin?()
    }
    
   @objc private func registerBtnClick(){
   vistorViewDelegation?.userWillRegister?()
    }
    
    //懒加载子视图控件
    //圆圈
   private lazy var cycleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
   private lazy var houseView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示文案
   private lazy var titleLabel: UILabel = {
        let l = UILabel()
        l.text = "关注一些人,你会知道的更多"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.textAlignment = NSTextAlignment.Center
        l.numberOfLines = 0
        l.sizeToFit()
        
        return l
        }()
    //登录按钮
     lazy var loginBtn: UIButton = {
       let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.titleLabel?.textAlignment = NSTextAlignment.Center
        return btn
    }()
    //注册按钮
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.textAlignment = NSTextAlignment.Center
        return btn
        }()
    //遮盖视图
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}
